/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.servlet;

import bc.entity.Bophan;
import bc.entity.Capbac;
import bc.entity.Congty;
import businesscard.DAO.LienheDAO;
import businesscard.DAO.UserDAO;
import bc.entity.Lienhe;
import bc.entity.Nganh;
import bc.entity.Quanhuyen;
import bc.entity.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Huyen
 */
@WebServlet(name = "AddContactController", urlPatterns = {"/AddContactController"})
public class AddContactController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
           String action = request.getParameter("action");
            String urlDispatcher = "";
            if (action.equals("Add Contact")) {
                String hoTen=request.getParameter("txtTen");
                Integer nganh = Integer.parseInt(request.getParameter("txtNganhnghe"));
                Nganh ng=new Nganh();
                ng.setIdNganh(nganh);
                
                String email = request.getParameter("txtEmail");
                String socd = request.getParameter("txtSocd");
                String sodd= request.getParameter("txtSodd");
                Integer quan=Integer.parseInt(request.getParameter("txtQuanhuyen"));
                Quanhuyen quanhuyen=new Quanhuyen();
                quanhuyen.setIdQuanhuyen(quan);
                Integer capbac = Integer.parseInt(request.getParameter("txtCapbac"));
                Capbac cb=new Capbac();
                cb.setIdCapbac(capbac);
                Integer id_bophan=Integer.parseInt(request.getParameter("txtBophan"));
                Bophan bophan=new Bophan();
                bophan.setIdBophan(id_bophan);
                Integer id_congty=Integer.parseInt(request.getParameter("txtCongty"));
                Congty congty=new Congty();
                congty.setIdCongty(id_congty);
                int sumlike=0;
                User user=(User)request.getSession().getAttribute("USER");
                String username=user.getUser();
                UserDAO userDAO=new UserDAO();    
                User user1=userDAO.getUser2(username);
                int point=user1.getPoint();
                int newpoint=point+10; 
                
                String diachi=request.getParameter("txtDiachi");
                String fax=request.getParameter("txtFax");
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date date1 = new Date(System.currentTimeMillis());
                
                String d=dateFormat.format(date1.getTime());
                Date date2=dateFormat.parse(d);
               // UserDAO userDAO=new UserDAO();
               // int id_user=userDAO.getUser(user);
                try{
                    UserDAO u2=new UserDAO();
                    u2.AddPoint(username, newpoint);
                    LienheDAO lhDAO=new LienheDAO();
                    Lienhe lh=new Lienhe(bophan,quanhuyen,user1,cb,ng,congty,hoTen,diachi,socd,sodd,fax,email,sumlike,date2);
                  //  Lienhe lh=new Lienhe(hoTen,gioiTinh,dt,id_quan,nganh,capbac,bophan,sodt,email,congty,id_user,sumlike);
                     lhDAO.Save(lh);
                     
                    urlDispatcher = "success.jsp";
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher(urlDispatcher);
            rd.forward(request, response);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {            
            out.close();
        }
    }
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
